﻿

namespace Mulala.Cavan.Core.Helper
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web;

    public class CookieHelper
    {
        public static void ClearCookie(string cookieName)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
            if (cookie != null)
            {
                cookie.Expires = DateTime.Now.AddHours(-48);
                HttpContext.Current.Response.Cookies.Set(cookie);
            }
        }


        public static void CreateCookie(string cookieName)
        {
            HttpCookie cookie = new HttpCookie(cookieName);
            HttpContext.Current.Response.Cookies.Add(cookie);
        }


        public static void CreateCookie(string cookieName, DateTime Expires)
        {
            HttpCookie cookie = new HttpCookie(cookieName);
            cookie.Expires = Expires;
            HttpContext.Current.Response.Cookies.Add(cookie);
        }

        public static string GetCookie(string key, string cookieName)
        {
            if (HttpContext.Current.Request.Cookies[cookieName] != null && HttpContext.Current.Request.Cookies[cookieName].Values[key] != null)
                return HttpContext.Current.Request.Cookies[cookieName].Values[key].ToString();
            return "";
        }

        public static string GetCookie(string key, string cookieName, bool bUseUrlDecode)
        {
            if (HttpContext.Current.Request.Cookies[cookieName] == null || HttpContext.Current.Request.Cookies[cookieName].Values[key] == null)
            {
                return "";
            }
            if (bUseUrlDecode)
            {
                return HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request.Cookies[cookieName].Values[key].ToString());
            }
            return HttpContext.Current.Request.Cookies[cookieName].Values[key].ToString();
        }

        public static bool IsExsit(string cookieName)
        {
            bool flag = false;
            if (HttpContext.Current.Request.Cookies[cookieName] != null)
            {
                flag = true;
            }
            return flag;
        }

        public static void SetCookie(string key, string value, string cookieName)
        {
            HttpCookie cookie = HttpContext.Current.Response.Cookies[cookieName]
                ?? HttpContext.Current.Request.Cookies[cookieName]
                ?? new HttpCookie(cookieName);
            
            cookie.Values[key] = value;
            HttpContext.Current.Response.Cookies.Set(cookie);

        }

        public static void SetCookie(string key, string value, bool encode, string cookieName)
        {
            HttpCookie cookie = HttpContext.Current.Response.Cookies[cookieName]
                ?? HttpContext.Current.Request.Cookies[cookieName]
                ?? new HttpCookie(cookieName);
            if (encode)
            {
                value = HttpContext.Current.Server.UrlEncode(value);
            }
            cookie.Values[key] = value;
            HttpContext.Current.Response.Cookies.Set(cookie);

        }

        public static void SetCookie(string cookieName, DateTime Expires)
        {
            HttpCookie cookie = HttpContext.Current.Response.Cookies[cookieName]
                ?? HttpContext.Current.Request.Cookies[cookieName]
                ?? new HttpCookie(cookieName);
            cookie.Expires = Expires;
            HttpContext.Current.Response.Cookies.Set(cookie);
        }
    }
}
